package com.zhigongtech.tender.rest.sys.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zhigongtech.common.exception.NomalException;
import com.zhigongtech.common.web.BaseController;
import com.zhigongtech.core.web.enm.NomalExceptionEnum;
import com.zhigongtech.core.web.jwt.JwtUtil;
import com.zhigongtech.extension.web.vo.Message;
import com.zhigongtech.tender.sys.entity.User;
import com.zhigongtech.tender.sys.service.IUserService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName:  LoginController   
 * @Description: 登录测试
 * @author: Alan
 * @date:   2019-06-12 14:06:45
 *     
 * @Copyright: 2019 www.richfit.com Inc. All rights reserved.
 */
@RestController
public class LoginController extends BaseController {

	@Autowired
	private IUserService userService;
	
	/**
	 * @Title: login   
	 * @Description: 登录功能 
	 * by Alan 2019-06-12 14:06:59
	 * @param user
	 * @return      
	 * @return  Message
	 */
	@PostMapping(value="/login")
	@ApiOperation(value="登录功能", notes="用户密码登录")
	@ApiModelProperty(name="user", value="用户密码json对象",required=true)
	public Message login(@RequestBody User user) {
		String username = user.getAccount();
		String password = user.getPassword();
		if(user == null || StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
			throw new NomalException(NomalExceptionEnum.USER_NOT_EXISTED);
		}
		Message msg = new Message();
		user = userService.getUserByAccount(username);
		if(user == null) {
			throw new NomalException(NomalExceptionEnum.USER_NOT_EXISTED);
		}
		if (userService.checkPasswd(password, user.getPassword())) {
			String token = JwtUtil.sign(username, user.getPassword());
			msg.addData("token", token);
			return msg.ok(200, "登录成功");
		}
		return msg.error(201, "登录失败");
	}
	

	/**
	 * 
	 * @Title: logout   
	 * @Description: 用户退出 
	 * by Alan 2019-06-12 15:22:44
	 * @param account
	 * @return      
	 * @return  Message
	 */
	@ApiImplicitParams({
    	@ApiImplicitParam(name = "account", value = "用户名", required = true, dataType = "String"),
	 })
	@GetMapping("/logout")
	public Message logout(@RequestParam("account") String account) {
		
		return null;
	}
	
}
